package com.fangzhi;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fangzhi.mobel.*;
import com.fangzhi.service.*;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.checkerframework.checker.units.qual.A;
import org.junit.jupiter.api.Test;
import org.mockito.internal.matchers.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.time.format.DateTimeFormatter;
import java.util.List;

@SpringBootTest
public class PrientExcel {

    @Autowired
    private OrdersService ordersService;
    @Autowired
    private OrderProductsService orderProductsService;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private ProductTypeService productTypeService;
    @Autowired
    private ProductTechnologyService productTechnologyService;

    public static void main(String[] args) throws IOException {
        String property = System.getProperty("user.dir");
        // 加载Excel模板
        FileInputStream fileInputStream = new FileInputStream(property + "/excel/excel2.xlsx");
        Workbook workbook = new XSSFWorkbook(fileInputStream);

        Sheet sheet = workbook.getSheetAt(0);
        Row row = sheet.getRow(1);
        // 订单号
        Cell cell = row.getCell(1);
        cell.setCellValue("P2024092490477");
        // 下单时间
        cell = row.getCell(4);
        cell.setCellValue("2024/09/24");
        // 客户名称
        row = sheet.getRow(2);
        cell = row.getCell(1);
        cell.setCellValue("客户名称");
        // 出货日期
        cell = row.getCell(4);
        cell.setCellValue("2024/09/24");



        FileOutputStream fileOutputStream = new FileOutputStream(property + "/excel/new.xlsx");
        workbook.write(fileOutputStream);
        workbook.close();
        fileOutputStream.close();
    }

    @Test
    public void start() throws IOException {
        String orderId = "P2024092404689";
        String property = System.getProperty("user.dir");


        Orders one = ordersService.getOne(Wrappers.<Orders>lambdaQuery().eq(Orders::getOrderId, orderId));
        Customer byId = customerService.getById(one.getCustomer());
        List<OrderProducts> list = orderProductsService.list(Wrappers.<OrderProducts>lambdaQuery().eq(OrderProducts::getOrderId, orderId));
        Integer productNum;
        if (list.size() <= 4){
            productNum = 5;
        }else if (list.size() <= 9){
            productNum = 10;
        }else{
            productNum = 15;
        }

        // 加载Excel模板
        FileInputStream fileInputStream = new FileInputStream(property + "/excel/excel2-"+productNum+".xlsx");
        Workbook workbook = new XSSFWorkbook(fileInputStream);
        Sheet sheet = workbook.getSheetAt(0);
        Row row = sheet.getRow(1);
        // 订单号
        Cell cell = row.getCell(1);
        cell.setCellValue(one.getOrderId());
        // 下单时间
        cell = row.getCell(4);
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
        cell.setCellValue(one.getCtime().format(dateTimeFormatter));
        // 客户名称
        row = sheet.getRow(2);
        cell = row.getCell(1);
        cell.setCellValue(byId.getName());
        // 出货日期
        cell = row.getCell(4);
        cell.setCellValue("");

        Integer rowNum = 4;
        for (OrderProducts orderProducts : list) {
            Integer price = 0;
            String productName1 = null;
            String productName2 = null;
            String productName3 = null;
            String productName4 = null;
            Long parentId = orderProducts.getProductTypeId();
            for (int i = 0; i < 4; i++) {
                ProductType productType = productTypeService.getById(parentId);
                if (i == 0) {
                    price = productType.getPrice();
                    productName1 = productType.getProductName();
                } else if (i == 1) {
                    productName2 = productType.getProductName();
                } else if (i == 2) {
                    productName3 = productType.getProductName();
                } else {
                    productName4 = productType.getProductName();
                }
                parentId = productType.getParentId();
            }

            row = sheet.getRow(rowNum);
            // 品名
            cell = row.getCell(0);
            cell.setCellValue(productName4 + "-" + productName3);
            // 规格尺寸
            cell = row.getCell(1);
            cell.setCellValue(productName2);
            // 材质要求
            cell = row.getCell(2);
            ProductTechnology productTechnology = productTechnologyService.getById(orderProducts.getTechnologyId());
            cell.setCellValue(productName1 + "-" + productTechnology.getName());
            // 数量
            cell = row.getCell(3);
            cell.setCellValue(orderProducts.getNum());
            // 单价

            cell = row.getCell(4);
            cell.setCellValue((price + productTechnology.getPrice()) / 100D);
            // 金额
            cell = row.getCell(5);
            cell.setCellValue(orderProducts.getTotalAmount() / 100D);
            rowNum += 1;
        }
        // 备注
        row = sheet.getRow(productNum + 4);
        cell = row.getCell(1);
        cell.setCellValue("备注");
        // 总额
        cell = row.getCell(5);
        cell.setCellValue(one.getTotalAmount() / 100D);

        // 收货地址
        row = sheet.getRow(productNum + 6);
        cell = row.getCell(1);
        cell.setCellValue(byId.getProvince() + byId.getCity() + byId.getArea() + byId.getDescs() + "      联系电话：" + byId.getTotal());
        FileOutputStream fileOutputStream = new FileOutputStream(property + "/excel/new.xlsx");
        workbook.write(fileOutputStream);
        workbook.close();
        fileOutputStream.close();
    }


}
